--技能按钮
--普通攻击
local Attack = MClass("Attack")

--ctor 初始化UI init设置静态数据 setData设置动态数据
function Attack:ctor()
    --self.root 默认为根节点
    self.img1 = self:findChild("Image_1")
    self.img2 = self:findChild("Image_2")
    self.img3 = self:findChild("Image_3")
    self.aniNode = self:findChild("ani")
    self.aniNode:setTouchEnabled(false)
    self.mSkillLoopAction = cc.CSLoader:createTimeline(Res.BattleSkillLoopEffect)
    if self.mSkillLoopAction then
        self.aniNode:runAction(self.mSkillLoopAction)
        self.mSkillLoopAction:gotoFrameAndPlay(0, self.mSkillLoopAction:getDuration(), true)
    end
    self:init()
end

function Attack:init()
    -- 持续普攻和自动战斗都需要播放绿色转圈动画
    local setting = UD.setting
    self:setAnimationVisible(setting:getIsAutoBattle() or setting:isKeepBasicAttack())

    local handleAuto = EM:on("onSettinAutoBattle", function(name, isAutoBattle)
        self:setAnimationVisible(isAutoBattle or setting:isKeepBasicAttack())
    end)
    BattleUIEvent:addEmHandle(handleAuto) 

    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_KEEPBKIL,handler(self,Attack.onSetKeepBasicAttack))

end

function Attack:onSetKeepBasicAttack(data)
    self:setAnimationVisible(data.isKeep or UD.setting:getIsAutoBattle())
end

function Attack:initType(classID)
    if classID == Enums.Profession.mage or classID == Enums.Profession.preist  then
    	self.img1:setVisible(false)
    	self.img2:setVisible(true)
        self.img3:setVisible(false)
    elseif classID == Enums.Profession.warrior or classID == Enums.Profession.rouge then
    	self.img1:setVisible(true)
    	self.img2:setVisible(false)
        self.img3:setVisible(false)
    elseif classID == Enums.Profession.hunter then
        self.img1:setVisible(false)
        self.img2:setVisible(false)
        self.img3:setVisible(true)
    else
        self.img1:setVisible(true)
        self.img2:setVisible(false)
        self.img3:setVisible(false)
    end
end

function Attack:setAnimationVisible(bShow)
    self.aniNode:setVisible(bShow)
end

function Attack:setData(data,classID)
	if not data then return end
    BattleUIEvent:addObjToFrameUpdate(self,Attack.updateFrame)
	self.classSkillId = data.classSkillId
	self.skillCfg =	data.skillCfg
	self:initType(classID)
    self.skillId = self.skillCfg:getId()

	local function clickCallBack(sender,eventType)
        if eventType == ccui.TouchEventType.began then
            self.startTime = math.floor(app.session:getServerTime() or app:getUTCTime())
            local tempX, tempY = self:getPosition()
            self.posX, self.posY = self:getParent():toWorldPosition(tempX, tempY)
            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO)
        elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
            self.startTime = nil
            self:setAnimationVisible(false or UD.setting:getIsAutoBattle() or UD.setting:isKeepBasicAttack())
            if eventType == ccui.TouchEventType.ended then
                BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SKILL_SELECT,{skillId = self.skillId, posX = self.posX})
            end
        end
	end

	self:addTouchEventListener(clickCallBack)


end

function Attack:updateFrame()
    --长按tips
    if not self.startTime then
        return
    end
    self.duleTime = math.floor(app.session:getServerTime() or app:getUTCTime())
    if self.duleTime-self.startTime > Constant.BattleCancelInterval then
        self:setAnimationVisible(true)
        self.startTime  = self.duleTime
        BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SKILL_SELECT,{skillId = self.skillId, posX = self.posX})
    end
end

function Attack:getSkillId()
	return self.skillCfg:getId()
end

return Attack